import java.util.Arrays;

public class TesSort {
    /**
     * 两个有序数组 合并为 一个有序数组
     *
     * @param array1
     * @param array2
     * @return
     */
    public static int[] mergeArray (int[] array1, int[] array2) {
        //在这里可以判断一下参数是否为空  不然如果array1 或者 array2 为空的话  那么 .length 就会报空指针异常
        int[] tmp = new int[array1.length + array2.length];
        int i = 0, j = 0, k = 0;
        for (; i < array1.length && j < array2.length;) {
            if (array1[i] <= array2[j]) {
                tmp[k++] = array1[i++];
            } else {
                tmp[k++] = array2[j++];
            }
        }
        while (i < array1.length) {
            tmp[k++] = array1[i++];
        }
        while (j < array2.length) {
            tmp[k++] = array2[j++];
        }
        return tmp;
    }

    /**
     * 归并排序
     * 时间复杂度:O(n * logn)
     * 空间复杂度:O(n)
     * 稳定性:稳定的排序  array[s1] <= array[s2]  不取等号  那么他就是不稳定的
     * 学过的稳定的排序 :
     * 冒泡  插入  归并
     * @param array
     */
    public static void mergeSort (int[] array) {
        mergeSortIntegernal(array,0,array.length - 1);
    }
    public static void mergeSortIntegernal (int[] array, int low, int high) {
        if (low >= high) {
            return;
        }

        int mid = low + ((high - low) >>> 1);
        //左边
        mergeSortIntegernal(array,low,mid);
        //右边
        mergeSortIntegernal(array,mid + 1,high);
        //合并
        merge(array,low,mid,high);
    }
    public static void merge (int[] array, int low, int mid, int high) {
        int[] tmp = new int[high - low + 1];
        int k = 0;
        int s1 = low;
        int e1 = mid;
        int s2 = mid + 1;
        int e2 = high;
        for (; s1 <= e1 && s2 <= e2;) {
            if (array[s1] <= array[s2]) {
                tmp[k++] = array[s1++];
            } else {
                tmp[k++] = array[s2++];
            }
        }
        while (s1 <= e1) {
            tmp[k++] = array[s1++];
        }
        while (s2 <= e2) {
            tmp[k++] = array[s2++];
        }
        for (int i = 0; i < k; i++) {
            array[i + low] = tmp[i];
        }
    }
    public static void mergeSort1 (int[] array) {
        int nums = 1;//每组的数据个数
        while (nums < array.length) {
            //数组每次都要进行遍历,确定要归并的区间
            for (int i = 0; i < array.length; i += nums * 2) {
                int left = i;
                int mid = left + nums - 1;
                //防止越界
                if (mid >= array.length) {
                    mid = array.length - 1;
                }
                int right = mid + nums;
                //防止越界
                if (right >= array.length){
                    right = array.length - 1;
                }
                //下标确定后  进行合并
                merge(array,left,mid,right);
            }
            nums *= 2;
        }
    }

    /**
     * 计数排序  :  一般适用于 n 个数 ,数据范围是 0 ~ n 之间的
     * 时间复杂度 : O(n)
     * 空间复杂度 : O(M) M : 代表 当前数据的范围 900 ~ 999
     * 稳定性 : 当前代码是不稳定的  本质是稳定的
     * @param array
     */
    public static void coutingSort (int[] array) {
        int maxVal = array[0];
        int minVal = array[0];
        for (int i = 0; i < array.length; i++) {
            if (minVal > array[i]) {
                minVal = array[i];
            }
            if (maxVal < array[i]) {
                maxVal = array[i];
            }
        }
        int[] count = new int[maxVal - minVal + 1]; //默认值为0
        for (int i = 0; i < array.length; i++) {
            int index = array[i];
            count[index - minVal]++;
        }
        //遍历数组  把数据写回array
        int indexArray = 0;
        for (int i = 0; i < count.length; i++) {
            while (count[i] != 0) {
                array[indexArray++] = i + minVal;
                count[i]--;
            }
        }
    }
    public static void main(String[] args) {
        int[] array1 = new int[]{1,2,3,4,5,9};
        int[] array2 = new int[]{2,3,4,5,7,8};
        int[] array = new int[]{1,2,3,4,5,9,2,3,4,5,7,8};
        /*int[] tmp = mergeArray(array1,array2);
        System.out.println(Arrays.toString(tmp));  //[1, 2, 2, 3, 3, 4, 4, 5, 5, 7, 8, 9]
        mergeSort(array);
        System.out.println(Arrays.toString(array));*/
        /*mergeSort1(array);
        System.out.println(Arrays.toString(array));*/
        coutingSort(array);
        System.out.println(Arrays.toString(array));
    }
}
